//
//  FXTreeNode.m
//  FetionDataStructure
//
//  Created by Ronnie on 08-12-2.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "FXTreeNode.h"


@implementation FXTreeNode

- (id)initLeafNodeWithUserInfo:(FXUserInfo*)user parent:(FXTreeNode*)parent
{
	self = [super init];
	user_info = [user retain];
	isLeaf = true;
	
	groupId = -1;
	groupName = nil;
	nodeChildren = [[NSMutableArray arrayWithArray:[NSArray array]] retain];
	nodeParent = parent;
	
	dialogWindowCtrl = nil;
	chatHistory = [[NSMutableArray arrayWithArray:[NSArray array]] retain];
	return self;
}

- (id)initGroupNodeWithId:(int)gId name:(NSString*)gName parent:(FXTreeNode*)parent
{
	self = [super init];
	user_info = nil;
	isLeaf = false;
	
	groupId = gId;
	groupName = [gName retain];
	nodeChildren = [[NSMutableArray arrayWithArray:[NSArray array]] retain];
	nodeParent = [parent retain];

	dialogWindowCtrl = nil;
	return self;
}

- (void) dealloc
{
	if (nodeChildren) {
		[nodeChildren release];
	}
	if (chatHistory) {
		[chatHistory release];
	}
	if (groupName) {
		[groupName release];
	}
	if (nodeParent) {
		[nodeParent release];
	}
	if (dialogWindowCtrl) {
		[dialogWindowCtrl release];
	}
	[super dealloc];
}

- (id)copyWithZone:(NSZone *)zone {
	return [self retain];
}


- (void)setParent:(FXTreeNode*)parent
{
	nodeParent = [parent retain];
}

- (DialogWindowController*)getDialogWindowContorller
{
	return dialogWindowCtrl;
}

- (void)setDialogWindowController:(DialogWindowController*)ctrl
{
	dialogWindowCtrl = [ctrl retain];
}
- (void)releaseDialogWindowController
{
	@synchronized(self)
	{
		[dialogWindowCtrl release];
		dialogWindowCtrl = nil;
	}
}

- (int)groupId
{
	return groupId;
}

- (FXTreeNode*)getNodeParent
{
	return nodeParent;
}

- (NSMutableArray*)getChatHistory
{
	return chatHistory;
}

- (int)numberOfChildren
{
	return [nodeChildren count];
}

- (void)insertChild:(FXTreeNode*)child atIndex:(int)index
{
	@synchronized(self)
	{
		[nodeChildren insertObject:child atIndex:index];
		[child setParent:self];
	}
}

- (FXTreeNode*)childAtIndex:(int)index
{
	return [nodeChildren objectAtIndex:index];
}

- (void)removeChild:(FXTreeNode*)object
{
	[object setParent:nil];
	[nodeChildren removeObject:object];
}

- (bool)isLeaf
{
	return isLeaf;
}
- (bool)isGroup
{
	return !isLeaf;
}
- (bool)isExpandable
{
	return [self isGroup];
}


- (FXUserInfo*)getUserInfo
{
	return user_info;
}


- (NSString*)getGroupName
{
	return groupName;
}

- (void)updateGroupName
{
	Fetion_Group *group = NULL;
	DList *group_list = (DList *)fx_get_group();
	
	while(group_list)
	{
		group = (Fetion_Group *) group_list->data;
		
		if(group->id == groupId) { 
			groupName = [[NSString stringWithUTF8String:group->name] retain];
			break;
		}
		group_list = d_list_next(group_list);
	}
	NSLog(@"Update failed!!!");
}

- (void)getMessage:(NSString*)msg
{
	
}

- (void)addAttributedString:(NSAttributedString*)string
{
	@synchronized(self)
	{
		[chatHistory insertObject:string atIndex:[chatHistory count]];
	}
}


- (NSComparisonResult)compare:(FXTreeNode*)other
{
	if ([self isGroup] && [other isGroup]) {
		/*if([self groupId] == [other groupId])
			return NSOrderedSame;
		else if ([self groupId] < [other groupId]) 
			return NSOrderedAscending;
		else
			return NSOrderedDescending;*/
		return [[self getGroupName] localizedCaseInsensitiveCompare:[other getGroupName]];
	}
	else
	{
		if ([self isGroup] && [other isLeaf]) {
			return NSOrderedDescending;
		}
		else if ([self isLeaf] && [other isGroup]) {
			return NSOrderedAscending;
		}
		else
		{
			if ([[self getUserInfo] userState] != [[other getUserInfo] userState]) {
				if([[self getUserInfo] userState] == [[other getUserInfo] userState])
					return NSOrderedSame;
				else if ([[self getUserInfo] userState] < [[other getUserInfo] userState]) 
					return NSOrderedAscending;
				else
					return NSOrderedDescending;
			}
			else {
				return [[[self getUserInfo] userName] localizedCaseInsensitiveCompare:[[other getUserInfo] userName]];
			}
		}
	}
}

- (void)recursiveSortChildren
{
    [nodeChildren sortUsingSelector:@selector(compare:)];
    [nodeChildren makeObjectsPerformSelector: @selector(recursiveSortChildren)];
}
- (void)sortChildren
{
	[nodeChildren sortUsingSelector:@selector(compare:)];
}

- (void)PrintTreeNode
{
	if (isLeaf) {
		NSLog(@"Leaf: "); 
		[user_info PrintUserInfo];
	}
	else
	{
		NSLog(@"Group: %i", [self numberOfChildren]);
	}
	
	FXTreeNode* treeNode = nil;
	for (treeNode in nodeChildren) {
		[treeNode PrintTreeNode];
	}
}

@end
